package com.directions.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.widget.EditText;

public class DialogUtils {

	public static void showConfirmationAlert(Activity activity, String message,
			String title) {
		showConfirmationAlert(activity, message, title, null);
	}

	public static void showConfirmationAlert(Activity activity, String message,
			String title, DialogInterface.OnClickListener listener) {

		// Do not build dialog if the activity is finishing
		if (!activity.isFinishing()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(activity)
					.setMessage(message)
					.setCancelable(true)
					.setTitle(title)
					.setPositiveButton(activity.getString(android.R.string.ok),
							listener);
			builder.create().show();
		}
	}

	public static void showItemsAlert(Activity activity, String title,
			String[] items, DialogInterface.OnClickListener listener) {

		// Do not build dialog if the activity is finishing
		if (!activity.isFinishing()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(activity)
					.setTitle(title).setItems(items, listener);
			builder.create().show();
		}
	}

	public static void getTextInput(Context context, String title, String hint,
			String text, final TextInputListener listener) {

		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setTitle(title);
		final EditText input = new EditText(context);
		if (text != null)
			input.setText(text);
		if (hint != null)
			input.setHint(hint);
		input.setSingleLine();
		alert.setView(input);
		alert.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						listener.input(input.getText().toString());
					}
				});
		alert.setNegativeButton(android.R.string.cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						listener.canceled();
					}
				});
		alert.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				listener.canceled();
			}
		});
		alert.show();
	}

	public static void showMessageDialog(Context ctx, String title,
			String text, final TextInputListener listener) {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx);

		alertDialogBuilder.setTitle(title);

		alertDialogBuilder.setMessage(text);

		// set positive button: Yes message

		alertDialogBuilder.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int id) {
						listener.input(null);

					}

				});

		// set negative button: No message

		alertDialogBuilder.setNegativeButton(android.R.string.cancel, null);

		AlertDialog alertDialog = alertDialogBuilder.create();

		alertDialog.show();
	}

	public interface TextInputListener {
		void input(String msg);

		void canceled();
	}
}
